﻿package 
{
	import bipede.animation.Marche;
	import bipede.animation.SceneManager;
	import bipede.animation.Timeline;
	import bipede.controleurs.Animateur;
	import bipede.user.Voiture;
	import bipede.membres.Person;
	import bipede.membres.PhysicJoint;
	
	import bipede.data.Allures;
	import events.ArmatureEvent;
	import events.UserEvent;
	import fab.geom.Cone;
	import fab.geom.Drawer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.*;
	import events.MoveEvent;
	import physic.*;
	import flash.display.Bitmap;
	import flash.display.BitmapData;	
	import flash.geom.Point;
	
	
	public class Main extends MovieClip
	{
		static public var instance:Main;
		static public var drawings:Sprite;
		static public var marche_setup:Sprite;
		static public var _mascotte:Person;
		private var _bloc:Obstacle;
		
		public function Main()
		{
			instance = this;
			addEventListener(Event.ADDED_TO_STAGE, init );
		}
		public function init(pEvt=null):void
		{
			_mascotte = (mascotte as Person);
			
			IKManager.trackAllArmatures(false);
			
			//_mascotte.
			addEventListener( ArmatureEvent.READY , _armature_ready );
			
			
			Engine.espace = this;
			SceneManager.register( mascotte );
			
			drawings = new Sprite();
			addChild( drawings );
			
			marche_setup = new Sprite();
			addChild( marche_setup );
			marche_setup.x = 100;
			marche_setup.y = 400;
			
			Drawer.drawPoint(drawings.graphics, new Point( marche_setup.x, marche_setup.y ), 0xFF0000);
			
			//record.addEventListener( MouseEvent.CLICK, _record );
			//applique.addEventListener( MouseEvent.CLICK, _applique );
			start_physics.addEventListener( MouseEvent.CLICK, Engine.start );
			stop_physics.addEventListener( MouseEvent.CLICK, Engine.stop );
			pause_physics.addEventListener( MouseEvent.CLICK, Engine.pause );
			next_physics.addEventListener( MouseEvent.CLICK, Engine.next );
			
			start_animation.addEventListener( MouseEvent.CLICK, Timeline.start );
			stop_animation.addEventListener( MouseEvent.CLICK, Timeline.stop );
			pause_animation.addEventListener( MouseEvent.CLICK, Timeline.pause );
			
			set_default_pose.addEventListener( MouseEvent.CLICK, _mascotte.set_default_pose );
			
			//start_marche.addEventListener( MouseEvent.CLICK, _mascotte.start_marche );
			
			for ( var i :int = 0; i <= 6; i++ )
			{
				this[ "s" + i ].addEventListener( MouseEvent.CLICK, _applique );
				this[ "t" + i ].addEventListener( MouseEvent.CLICK, _record );
			}
			
			dragg.lockCenter = true;
			dragg.addEventListener( MoveEvent.STARTMOVING, _mascotte.startmoving );
			dragg.addEventListener( MoveEvent.ENDMOVING, _mascotte.endmoving );
			dragg.addEventListener( MoveEvent.MOVING, _mascotte.move );// moving );
			
			
		}
		private function _armature_ready( event:ArmatureEvent ):void
		{
			trace ( event.target.name + " Ready" );

			switch( event.target.name )
			{
				case "mascotte" :
				
				//_mascotte.create_poignee_bone( "yeux" );
				//_mascotte.create_poignee_bone( bouche );
				//_mascotte.create_poignee_bone( "bone_mainD" );
				//_mascotte.create_poignee_bone( "bone_mainG" );
				//_mascotte.create_poignee_bone( "bone_tibiaG" );
				//_mascotte.create_poignee_bone( "bone_tibiaD" );
				//_mascotte.create_poignee_bone( "bone_piedG" );
				//_mascotte.create_poignee_bone( "bone_piedD" );
				
				//_mascotte.create_physic_joint( "bone_piedG" );
				//_mascotte.create_physic_joint( "bone_piedD" );
				//_mascotte.create_physic_joint( "bone_tibiaG" );
				//_mascotte.create_physic_joint( "bone_tibiaD" );				
				
				_mascotte.physics = Engine.registerSoftBody( _mascotte );
				_bloc = Engine.registerObstacle( bloc1, new Point( 0,0 ), new Point( 100,30 ) );
				var _bloc2:Obstacle = Engine.registerObstacle( bloc2, new Point( 0,0 ), new Point( 100,-20 ) );
				var _bloc3:Obstacle = Engine.registerObstacle( bloc3, new Point( 0,0 ), new Point( 100,80 ) );
				var _bloc4:Obstacle = Engine.registerObstacle( bloc4, new Point( 0, 0 ), new Point( 100, -60 ) );
				
				Main.drawings.graphics.lineStyle( 2, 0x333333 );
				//Engine.start();
			
				//_mascotte.release_joint_constraint( "bone_mainD", 4 );
				//_mascotte.release_joint_constraint( "bone_mainG", 4 );
				
				/*
				var c1:Contrainte = _mascotte.create_contrainte( new Point( 360, 200 ), "bone_mainD", "bone_epauleD" );
				var c2:Contrainte = _mascotte.create_contrainte( new Point( 420, 220 ), "bone_mainG", "bone_epauleG" );
				
				contrainte1.lockCenter = true;
				//contrainte1.addEventListener( MoveEvent.STARTMOVING, _mascotte.startmoving );
				//contrainte1.addEventListener( MoveEvent.ENDMOVING, _mascotte.endmoving );
				contrainte1.addEventListener( MoveEvent.MOVING, c1.update );		
				contrainte2.lockCenter = true;
				contrainte2.addEventListener( MoveEvent.MOVING, c2.update );	
				*/
				
				//_mascotte.orientation = false;
				_mascotte.repose( new MoveEvent( MoveEvent.REPOSE, null, _bloc ) );
			
				//_mascotte.start_marche( _bloc );
				//var m:Marche = new Marche( _mascotte, true );
				//obtain_walkcycle.addEventListener( MouseEvent.CLICK, m.obtain_walkcycle );
				
				// MARCHE
				
				var anim:Animateur = new Animateur( _mascotte );
				anim.action( "marche" );
				anim.configure( { 
									support:_bloc, 
									distance_support:_mascotte.distance_support, 
									nombre_pas:0, 
									pasFrames:12, 
									walkcycle:Allures.walkcycle, 
									recul_pied_devant:58, 
									longueur_pas:120, 
									vitesse:new Point(8, 0), 
									amplitude: -14, 
									phase:120 * 2
					} );
				anim.start();
				
				
				// LEVE BRAS
/*
				var leve_les_bras:Animateur = new Animateur( _mascotte );
				leve_les_bras.action( "leve_les_bras" );
				leve_les_bras.configure( { 
									vitesse:.05, 
									cible:new Point(0, -240 ),
									main_droite:_mascotte.main_droite,
									main_gauche:_mascotte.main_gauche
				} );
				leve_les_bras.start();
*/
				
				// bras ballants

				var posture_base:Animateur = new Animateur( _mascotte );
				posture_base.action( "bras_ballants" );
				posture_base.configure( { 
									support:_bloc, 
									distance_support:_mascotte.distance_support, 
									amplitude: -2, 
									phase:4,		
									vitesse:.1
				} );
				//posture_base.start();
				
				stage.addEventListener( MouseEvent.MOUSE_MOVE, _mascotte.yeux.mouse_move );
				
				var key:Voiture = new Voiture();
				key.addEventListener( UserEvent.DOWN, _key_down );
				key.addEventListener( UserEvent.UP, _key_up );
				key.addEventListener( UserEvent.RIGHT, _key_right );
				key.addEventListener( UserEvent.LEFT, _key_left );
				
				// test "voir les obstacles"
				var image:Bitmap = new Bitmap( Engine.zone_obstacles );
				addChild(image);
			
				break;
			}
		}
		
		// user commande
		
		private function _mouse_move(event:MouseEvent):void 
		{
		}
		private function _key_down(e:UserEvent):void 
		{
			if ( _mascotte.animateurs.marche != null )
			{
				_mascotte.animateurs.marche.stop();
				_mascotte.animateurs.bras_ballants.configure( { 
									orientation:_mascotte.orientation
					} );				
				_mascotte.animateurs.bras_ballants.start();
			}
		}
		
		private function _key_up(e:UserEvent):void 
		{
			
		}
		private function _key_left(e:UserEvent):void 
		{
			if (!_mascotte.orientation && _mascotte.animateurs.marche.running ) return;
			trace("_key_left");
			_mascotte.orientation = false;
			_start_marche();		
		}
		
		private function _key_right(e:UserEvent):void 
		{
			if (_mascotte.orientation && _mascotte.animateurs.marche.running ) return;
			trace("_key_right");
			_mascotte.orientation = true;
			_start_marche();
		}
		private function _start_marche():void 
		{
			if ( _mascotte.animateurs.marche != null )
			{
				//_mascotte.repose( new MoveEvent( MoveEvent.REPOSE, _mascotte.place, _bloc ) );
				_mascotte.animateurs.marche.configure( { 
									orientation:_mascotte.orientation
					} );
				_mascotte.animateurs.bras_ballants.stop();
				_mascotte.animateurs.marche.start();
			}
		}
		
		// framing
		
		private function _record( event:MouseEvent ):void
		{
			if ( event.currentTarget.name == "record" )
			{
				(mascotte as Person).record();
			}
			else {
				Timeline.record( int( event.currentTarget.name.substring(1) ), (mascotte as Person ).record() );
			}
		}		
		private function _applique( event:MouseEvent ):void
		{
			if ( event.currentTarget.name == "record" )
			{
				mascotte.applique();
			}
			else {
				Timeline.show( int( event.currentTarget.name.substring(1) ), false );
			}			
		}	
	}
}